<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'
import { queryRigth44 } from '@/api/business/largeScreen'

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '200px'
    }
  },
  data() {
    return {
      chart: null,
      colors: ['#FFCC67', '#FFF68F']
    }
  },
  mounted() {
    this.initChart('03')
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(zhlb) {
      this.chart = echarts.init(this.$el, 'macarons')

      queryRigth44({ zhlb: zhlb }).then(res => {
        console.log('queryRigth4:{}', res)
        this.chart.setOption({
          title: {
            text: '居家各参保类型人员数量',
            // left: 20,
            textStyle: {
              color: '#FFFFFF',
              fontSize: 14,
              fontWight: 400
            }
          },
          tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b} : {c} ({d}%)'
          },
          color: this.colors,
          /*          legend: {
            orient: 'vertical',
            right: 10,
            textStyle: {
              color: '#FFFFFF',
              fontSize: 12
            },
            icon: 'roundRect',
            /!*   // 图例
               show: true,
               textStyle: {
                 color: '#FFFFFF'
               },
               orient: 'horizontal',
               top: 'bottom',
               itemGap: 10,*!/
            // padding: [20, 10, 0, 10],
            data: res.legendNameArray
          },*/
          calculable: true,
          series: [{
            name: '失能等级',
            type: 'pie',
            label: {
              // 悬浮提示文字
              normal: {
                // show: false,
                position: 'inner',
                formatter: '{b} \n  {c}人',
                textStyle: {
                  color: '#000000',
                  fontWeight: 'bold',
                  fontSize: 12
                }
              }
            },
            // radius: [50, 250],
            radius: '70%',
            center: ['50%', '50%'],
            itemStyle: {
              borderRadius: 8
            },
            data: res.pieArray
          }]
        })
      })
    }
  }
}
</script>
